package main.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.HashMap;
import java.util.List;
import main.model.DataModel;
import main.model.NullModel;


/**
 * A canvas to visualize the data.
 *
 * @author Yaqi Zhang
 * @author Seon Kang
 */

public class Visualizer {
    private static final int LEFTMARGIN = 40;
    private static final int BOTTOMMARGIN = 40;
    private DataModel myData;
    private int myLeftMargin;
    private int myBottomMargin;
    private int myHeight = 0;
    private int myWidth = 0;
    private String myXAxis = "";
    private String myYAxis = "";

    /**
     *
     * @param data model
     * @param size visualizer size
     */
    public Visualizer (DataModel data, Dimension size) {
        setLeftMargin(LEFTMARGIN);
        setBottomMargin(BOTTOMMARGIN);
        setData(data);
        setSize(size);
    }

    protected void setSize (Dimension size) {
        myHeight = size.height;
        myWidth = size.width;
    }

    protected int getMyHeight () {
        return myHeight;
    }

    protected int getMyWidth () {
        return myWidth;
    }

    /**
     * Sets this visualizers data
     *
     * @param data data to set use
     */
    public void setData (DataModel data) {
        myData = data;
    }

    /**
     *
     * @return
     */
    public DataModel getData () {
        if (myData == null) { return new NullModel(); }
        return myData;
    }

    /**
     *
     * @param pen Pen
     */
    public void paint (Graphics pen) {
        paintAxes(pen);
    }

    protected void paintAxes (Graphics pen) {
        pen.setColor(Color.BLACK);
        pen.drawLine(0, myHeight - myBottomMargin, myWidth, myHeight - myBottomMargin);
        pen.drawLine(myLeftMargin, 0, myLeftMargin, myHeight);
        if (myData != null) {
            pen.drawString(myData.getName(), myWidth / 2, myBottomMargin);
        }
        pen.drawString(myXAxis, myWidth / 2, myHeight - 2);
        pen.drawString(myYAxis, 1, myBottomMargin);
    }

    protected void labelYAxis (int numberOfMarks, float yValueMax,
                               float baseYValue, Graphics myPen) {
        if (yValueMax == 0) { return; }
        float myVerticalIncrement = (getMyHeight() - getBottomMargin()) / yValueMax;
        myPen.setColor(Color.black);
        for (int i = 0; i <= numberOfMarks; i++) {
            myPen.drawString((int) ((yValueMax / numberOfMarks) * i + baseYValue) + "", 2,
                             getMyHeight() - getBottomMargin() -
                                     (int) (myVerticalIncrement * i * (yValueMax / numberOfMarks)));
        }
    }

    protected void setMyXAxis (String x) {
        myXAxis = x;
    }

    protected void setMyYAxis (String y) {
        myYAxis = y;
    }

    protected String getMyXAxis () {
        return myXAxis;
    }

    protected String getMyYAxis () {
        return myYAxis;
    }

    protected void setLeftMargin (int n) {
        myLeftMargin = n;
    }

    protected void setBottomMargin (int n) {
        myBottomMargin = n;
    }

    protected int getLeftMargin () {
        return myLeftMargin;
    }

    protected int getBottomMargin () {
        return myBottomMargin;
    }

    /**
     * @return
     */
    public HashMap<Integer, List<Object>> getPointMap () {
        return null;
    }
}
